﻿using System;
using Business;
using Entities;
namespace ServicesDirectory.UserControl
{
    public partial class EditProgramme : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    var currentProgramme = ProgrammeBusiness.Instance.GetProgrammeById(Request.QueryString["id"]);
                    TxtDescription.Text = currentProgramme.Description;
                    TxtProgrammeName.Text = currentProgramme.ProgrammeName;
                    HfProgrammeId.Value = currentProgramme.ProgrammeId.ToString();
                    TxtContact.Text = GetContactName(currentProgramme.ContactId.ToString());
                    CbActive.Checked = true;
                    currentProgramme.Active = true;
                    ProgrammeBusiness.Instance.Update(currentProgramme);

                }
            }
            lbtLookupContact.Click += (LookupContact_Click);
            LookupContactManager1.GetManager += GetManager;
            LookupContactManager1.CloseControl += LookupContactManager1_CloseControl;
            btnDeActive.Click += btnDeActive_Click;
        }

        /// <summary>
        /// Handles the CloseControl event of the LookupContactManager1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupContactManager1_CloseControl(object sender, EventArgs e)
        {
            pnlLookupContact.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the btnDeActive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void btnDeActive_Click(object sender, EventArgs e)
        {
            var programme = new Programme();
            if (ProgrammeBusiness.Instance.UniqueProgrammeName(TxtProgrammeName.Text, new Guid(HfProgrammeId.Value)))
            {
                programme.ProgrammeId = new Guid(HfProgrammeId.Value);
                if (!string.IsNullOrEmpty(lbtLookupContact.CommandArgument))
                    programme.ContactId = new Guid(lbtLookupContact.CommandArgument);
                programme.ProgrammeName = TxtProgrammeName.Text;
                programme.Description = TxtDescription.Text;
                programme.Active = false;

                var result = ProgrammeBusiness.Instance.Update(programme);
                if (result)
                    LblStatus.Text = "Successfully";
            }
            else
            {
                LblStatus.Text = "Same Programme Name";
            }
        }

        /// <summary>
        /// Gets the manager.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void GetManager(object sender, EventArgs e)
        {
            TxtContact.Text = GetContactName(sender.ToString());
            lbtLookupContact.CommandArgument = sender.ToString();
            pnlLookupContact.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the LookupContact control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LookupContact_Click(object sender, EventArgs e)
        {
            pnlLookupContact.Visible = true;
        }

        /// <summary>
        /// Handles the Click event of the Save control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Save_Click(object sender, EventArgs e)
        {
            var programme = new Programme();
            if (ProgrammeBusiness.Instance.UniqueProgrammeName(TxtProgrammeName.Text, new Guid(HfProgrammeId.Value)))
            {
                programme.ProgrammeId = new Guid(HfProgrammeId.Value);
                if (!string.IsNullOrEmpty(lbtLookupContact.CommandArgument))
                    programme.ContactId = new Guid(lbtLookupContact.CommandArgument);
                programme.ProgrammeName = TxtProgrammeName.Text;
                programme.Description = TxtDescription.Text;
                programme.Active = true;

                var result = ProgrammeBusiness.Instance.Update(programme);
                if (result)
                    LblStatus.Text = "Successfully";
            }
            else
            {
                LblStatus.Text = "Same Programme Name";
            }
        }

        /// <summary>
        /// Handles the Click event of the Back control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Back_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListProgramme.aspx");
        }

        /// <summary>
        /// Gets the name of the contact.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        private string GetContactName(string id)
        {
            string message;
            var contac = ContactBusiness.Instance.GetContact(id, out message);
            lbtLookupContact.CommandArgument = contac.ContactId.ToString();
            return contac.FirstName + " " + contac.SurName;

        }
    }
}